<%include ../includes/header.html %>
<style type="text/css">
    div#canvas-frame {
        border: none;
        cursor: pointer;
        width: 100%;
        height: 500px;
        background-color: #EEEEEE;
        position: relative;
    }

</style>
    <section class="content-header">
      <h1>
        初级教程
        <small>第3章 让场景动起来</small>
      </h1>
      <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
        <li class="active">Junior</li>
      </ol>
    </section>

    <!-- Main content -->
    <section class="content">
        <div id="canvas-frame"></div>
    </section>
    <!-- /.content -->
<%include ../includes/footer.html %>
<script>
  $(function(){
    // 渲染
    var renderer;
    // 性能监视器
    var stats;
    function initThree() {
        width = document.getElementById('canvas-frame').clientWidth;
        height = document.getElementById('canvas-frame').clientHeight;
        renderer = new THREE.WebGLRenderer({
            antialias : true
        });
        renderer.setSize(width, height);
        document.getElementById('canvas-frame').appendChild(renderer.domElement);
        renderer.setClearColor(0xFFFFFF, 1.0);

        stats = new Stats();
        stats.domElement.style.position = 'absolute';
        stats.domElement.style.left = '0px';
        stats.domElement.style.top = '0px';
        document.getElementById('canvas-frame').appendChild(stats.domElement);
    }

    // 相机
    var camera;
    function initCamera() {
        // 透视投影相机
        camera = new THREE.PerspectiveCamera(45, width / height, 1, 1000);
        // 正投影相机
        // camera = new THREE.OrthographicCamera( width / - 2, width / 2, height / 2, height / - 2, 1, 650 );
        camera.position.x = 0;
        camera.position.y = 0;
        camera.position.z = 800;
        camera.up.x = 0;
        camera.up.y = 1;
        camera.up.z = 0;
        camera.lookAt(0,0,0);
    }

    // 场景
    var scene;
    function initScene() {
        scene = new THREE.Scene();
    }

    // 灯光
    var light;
    function initLight() {
        light = new THREE.AmbientLight(0xFFFFFF);
        light.position.set(100, 100, 200);
        scene.add(light);
        light = new THREE.SpotLight(0x00FF00);
        light.position.set(0, 0,300);
        scene.add(light);
    }

    // 网格物体
    var mesh;
    function initObject() {
        // 几何形状
        var geometry = new THREE.CylinderGeometry( 100,150,400, 100);
        // 材质
        var material = new THREE.MeshLambertMaterial( { color:0xFFFF00} );
        mesh = new THREE.Mesh( geometry,material);
        mesh.position = new THREE.Vector3(0,0,0);
        scene.add(mesh);
    }

    // 动画引擎
    function initTween() {
        new TWEEN.Tween( mesh.position) // 移动的物体
                .to( { z: 400}, 5500 ).repeat( Infinity ).start();
    }

    function threeStart() {
        initThree();
        initCamera();
        initScene();
        initLight();
        initObject();
        initTween();
        animation();

    }
    function animation()
    {
        // 改变相机位置实现动画效果
        // camera.position.x =camera.position.x +1;
        // 改变物体位置实现动画效果
        // mesh.position.x +=1
        // 渲染
        renderer.render(scene, camera);
        requestAnimationFrame(animation);
        // 启动性能监控
        stats.update();
        // 使用tween动画引擎实现动画效果
        TWEEN.update();
    }

    threeStart();
  })
</script>
